import {
  createStore,
  Store,
  AnyAction,
  Action,
  applyMiddleware,
  StoreEnhancer,
  StoreEnhancerStoreCreator,
} from "redux";
import reducers, { CombinedState } from "./reducers";
import thunk, { ThunkDispatch } from "redux-thunk";

interface Ext {}
interface StateExt {}
interface ExtraArgument {}
export type ThunkAction<R, S, E, A extends Action> = (
  dispatch: ThunkDispatch<S, E, A>,
  getState: () => S,
  extraArgument: E
) => R;

type Ext1 = ThunkDispatch<CombinedState, ExtraArgument, Action>;
//Ext1是一个增强版的Dispatch 可以让我们向仓库派发函数
//applyMiddleware 返回的是一个Store增强器
let storeEnhancer: StoreEnhancer<{ dispatch: Ext1 }> = applyMiddleware<
  Ext1,
  CombinedState
>(thunk);
//增强过的store创建器 增强过后的createStore方法
let storeEnhancerStoreCreator: StoreEnhancerStoreCreator<
  Ext,
  StateExt
> = storeEnhancer(createStore);
//调用新的createStore方法得到一个增强后store
let store: Store<CombinedState & StateExt, AnyAction> &
  Ext = storeEnhancerStoreCreator(reducers);

/* let store: Store<CombinedState, AnyAction> & Ext = createStore<
  CombinedState,
  AnyAction,
  Ext,
  StateExt
>(
  reducers,
  applyMiddleware<
    ThunkDispatch<CombinedState, ExtraArgument, Action>,
    CombinedState
  >(thunk)
); */

//console.log(store.persist);
//console.log(store.getState().router);

/* export default store;

//createStore
function createStore() {}
//store enchancer
function enchancer(createStore: any) {
  return function () {
    //返回一个新的createStore方法
    let store = createStore();
    return {
      store,
      name: "zhufeng",
    };
  };
}
let newCreateStore = enchancer(createStore);
let store = newCreateStore();
 */
